import 'package:flutter/material.dart';

class FloatingMoveView extends StatefulWidget {
  final GestureTapCallback? tapCallBack;
  final GestureTapCallback? closeCallBack;
  final bool isShowClose;
  final String imagePath;
  final double bottom;
  final double right;
  final double imageWidth;
  final double imageHeight;
  final bool isCanMove;

  FloatingMoveView({
    Key? key,
    this.isShowClose = false,
    this.isCanMove = false,
    required this.imagePath,
    required this.imageWidth,
    required this.imageHeight,
    required this.bottom,
    required this.right,
    this.tapCallBack,
    this.closeCallBack,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _FloatingMoveViewState();
  }
}

class _FloatingMoveViewState extends State<FloatingMoveView> {
  final _floatingGlobalKey = GlobalKey();
  double _bottom = 16;
  double _right = 16;
  double? _width;

  double get _adSize => 70;

  double get width {
    if (_width == null) {
      var renderBox = _floatingGlobalKey.currentContext?.findRenderObject() as RenderBox?;
      _width = renderBox?.size.width ?? MediaQuery.of(context).size.width;
      _height = renderBox?.size.height ?? MediaQuery.of(context).size.height;
    }
    return _width ?? 0;
  }

  double? _height;

  double get height {
    if (_height == null) {
      var renderBox = _floatingGlobalKey.currentContext?.findRenderObject() as RenderBox?;
      _width = renderBox?.size.width ?? MediaQuery.of(context).size.width;
      _height = renderBox?.size.height ?? MediaQuery.of(context).size.height;
    }
    return _height ?? 0;
  }

  @override
  void initState() {
    super.initState();
    // _right = screen.screenWidth - _adSize - 16;
    _right = widget.right;
    _bottom = widget.bottom;
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _loadAdData();
    });
  }

  void _loadAdData() async {
    setState(() {});
  }

  void _onPanUpdate(DragUpdateDetails details) {
    if (!widget.isCanMove) return;
    _right -= details.delta.dx;
    _bottom -= details.delta.dy;
    if (_right > width - _adSize) {
      _right = width - _adSize;
    }
    if (_right < 0) _right = 0;
    if (_bottom > height - _adSize) {
      _bottom = height - _adSize;
    }
    if (_bottom < 0) _bottom = 0;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    double width = widget.imageWidth + 15;
    double height = widget.imageHeight + 15;
    return Container(
      key: _floatingGlobalKey,
      child: Stack(
        children: [
          Positioned(
            bottom: _bottom,
            right: _right,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(8),
              child: Container(
                height: width,
                width: height,
                child: Stack(
                  children: [
                    Positioned(
                      bottom: 10,
                      right: 18,
                      child: GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTapUp: (details) {
                          if (widget.tapCallBack != null) {
                            widget.tapCallBack?.call();
                          }
                        },
                        onPanUpdate: (DragUpdateDetails details) {
                          _onPanUpdate(details);
                        },
                        child: Image.asset(widget.imagePath, width: widget.imageWidth),
                      ),
                    ),
                    if (widget.isShowClose)
                      Positioned(
                        top: 2,
                        right: 12,
                        child: InkWell(
                          onTap: widget.closeCallBack,
                          child: Image.asset(
                            "assets/images/alert_white_close.png",
                            width: 20,
                            height: 20,
                          ),
                        ),
                      ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
